home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / pl-read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-24  |  47.8 KB  |  2,014 lines

  1. /*  $Id: pl-read.c,v 1.53 1998/03/24 13:39:13 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6. */
  7.  
  8. #include <math.h>
  9. /*#define O_DEBUG 1*/
  10. #include "pl-incl.h"
  11. #include "pl-ctype.h"
  12.  
  13. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. This module defines the Prolog parser.  Reading a term takes two passes:
  15.  
  16.     * Reading the term into memory, deleting multiple blanks, comments
  17.       etc.
  18.     * Parsing this string into a Prolog term.
  19.  
  20. The separation has two reasons: we can call the  first  part  separately
  21. and  insert  the  read  strings in the history and we can produce better
  22. error messages as the parsed part of the source is available.
  23.  
  24. The raw reading pass is quite tricky as PCE requires  us  to  allow  for
  25. callbacks  from  C  during  this  process  and the callback might invoke
  26. another read.  Notable raw reading needs to be studied studied once more
  27. as it  takes  about  30%  of  the  entire  compilation  time  and  looks
  28. promissing  for  optimisations.   It  also  could  be  made  a  bit more
  29. readable.
  30.  
  31. This module is considerably faster when compiled  with  GCC,  using  the
  32. -finline-functions option.
  33. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34.  
  35. forwards void    startRead(void);
  36. forwards void    stopRead(void);
  37. forwards void    errorWarning(char *);
  38. forwards void    singletonWarning(atom_t *, int);
  39. forwards void    clearBuffer(void);
  40. forwards void    addToBuffer(int);
  41. forwards char *    raw_read2(void);
  42. forwards char *    raw_read(void);
  43.  
  44. typedef struct token * Token;
  45. typedef struct variable * Variable;
  46.  
  47. struct token
  48. { int type;            /* type of token */
  49.   int start;            /* start-position */
  50.   int end;            /* end-position */
  51.   union
  52.   { number    number;        /* int or float */
  53.     atom_t    atom;        /* atom value */
  54.     term_t    term;        /* term (list or string) */
  55.     int        character;    /* a punctuation character (T_PUNCTUATION) */
  56.     Variable    variable;    /* a variable record (T_VARIABLE) */
  57.   } value;            /* value of token */
  58. };
  59.  
  60.  
  61. struct variable
  62. { char *    name;        /* Name of the variable */
  63.   term_t    variable;    /* Term-reference to the variable */
  64.   int        times;        /* Number of occurences */
  65.   word        signature;    /* Pseudo atom */
  66.   Variable     next;        /* Next of chain */
  67. };
  68.  
  69.  
  70. #define T_FUNCTOR    0    /* name of a functor (atom, followed by '(') */
  71. #define T_NAME        1    /* ordinary name */
  72. #define T_VARIABLE    2    /* variable name */
  73. #define T_VOID        3    /* void variable */
  74. #define T_NUMBER    4    /* integer or float */
  75. #define T_STRING    5    /* "string" */
  76. #define T_PUNCTUATION    6    /* punctuation character */
  77. #define T_FULLSTOP    7    /* Prolog end of clause */
  78.  
  79. extern int Input;        /* current input stream (from pl-file.c) */
  80. static unsigned char *here;        /* current character */
  81. static unsigned char *base;        /* base of clause */
  82. static unsigned char *token_start;    /* start of most recent read token */
  83. static char *last_syntax_error; /* last syntax error */
  84. static struct token token;    /* current token */
  85. static bool unget = FALSE;    /* unget_token() */
  86.  
  87. #define CHARESCAPE trueFeature(CHARESCAPE_FEATURE)
  88.  
  89. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  90. The reading function (raw_read()) can  be   called  recursively  via the
  91. notifier when running under event-driven packages  (like PCE).  To avoid
  92. corruption of the database we push the read buffer rb on a stack and pop
  93. in back when finished.  See raw_read() and raw_read2().
  94. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  95.  
  96. #define RBSIZE    512        /* initial size of read buffer */
  97. #define MAX_READ_NESTING 5    /* nesting of read (O_PCE only) */
  98.  
  99. static
  100. struct read_buffer
  101. { int    size;            /* current size of read buffer */
  102.   int    left;            /* left space in read buffer */
  103.   unsigned char *base;        /* base of read buffer */
  104.   unsigned char *here;        /* current position in read buffer */
  105.   int   stream;            /* stream we are reading from */
  106. } rb;
  107.  
  108. #if O_PCE
  109. static struct read_buffer rb_stack[MAX_READ_NESTING];
  110. int read_nesting = 0;        /* current nesting level */
  111. #endif /* O_PCE */
  112.  
  113. void
  114. resetRead(void)
  115. #if O_PCE
  116.   read_nesting = 0;
  117. #endif
  118. }
  119.  
  120. static
  121. void
  122. startRead(void)
  123. {
  124. #if O_PCE
  125.   if (read_nesting >= MAX_READ_NESTING)
  126.   { warning("Read stack too deeply nested");
  127.     pl_abort();
  128.   }
  129.   rb_stack[read_nesting++] = rb;
  130.   rb = rb_stack[read_nesting];
  131. #endif /* O_PCE */
  132.   rb.stream = Input;
  133.   source_file_name = currentStreamName();
  134. }
  135.  
  136. static void
  137. stopRead(void)
  138. {
  139. #if O_PCE
  140.   rb_stack[read_nesting] = rb;
  141.   rb = rb_stack[--read_nesting];
  142.   if (read_nesting < 0)
  143.     fatalError("Read stack underflow???");
  144. #endif /* O_PCE */
  145. }
  146.  
  147.         /********************************
  148.         *         ERROR HANDLING        *
  149.         *********************************/
  150.  
  151. static int give_syntaxerrors = TRUE;
  152.  
  153. int
  154. syntaxerrors(int new)
  155. { int old = give_syntaxerrors;
  156.   give_syntaxerrors = new;
  157.  
  158.   return old;
  159. }
  160.  
  161.  
  162. word
  163. pl_syntaxerrors(term_t old, term_t new)
  164. { return setBoolean(&give_syntaxerrors, "syntaxerrors", old, new);
  165. }
  166.  
  167.  
  168. #define syntaxError(what) { errorWarning(what); fail; }
  169.  
  170. static void
  171. errorWarning(char *what)
  172. { unsigned int c;
  173.   
  174.   if ( !give_syntaxerrors )
  175.   { source_char_no += token_start - base;
  176.     last_syntax_error = what;
  177.     return;
  178.   }
  179.  
  180.   c = *token_start;
  181.  
  182.   if ( !ReadingSource )            /* not reading from a file */
  183.   { Sfprintf(Serror, "\n[WARNING: Syntax error: %s \n", what);
  184.     *token_start = EOS;
  185.     Sfprintf(Serror, "%s\n** here **\n", base);  
  186.     if (c != EOS)
  187.     { *token_start = c;
  188.       Sfprintf(Serror, "%s]\n", token_start);
  189.     }
  190.   } else
  191.   { predicate_t pred = PL_pred(FUNCTOR_exception3, MODULE_user);
  192.     int rval;
  193.     unsigned char *s;
  194.  
  195.     for(s = base; s < token_start; s++ )
  196.     { if ( *s == '\n' )
  197.           source_line_no++;
  198.     }
  199.     
  200.     if ( pred->definition->definition.clauses )
  201.     { fid_t cid = PL_open_foreign_frame();
  202.       qid_t qid;
  203.       term_t argv = PL_new_term_refs(3);
  204.       term_t a    = PL_new_term_ref();
  205.     
  206.       PL_put_atom(    argv+0, ATOM_syntax_error);
  207.       PL_put_functor( argv+1, FUNCTOR_syntax_error3);
  208.       PL_put_variable(argv+2);
  209.       PL_get_arg(1, argv+1, a); PL_unify_atom(a, source_file_name);
  210.       PL_get_arg(2, argv+1, a); PL_unify_integer(a, source_line_no);
  211.       PL_get_arg(3, argv+1, a); PL_unify_atom_chars(a, what);
  212.            
  213.       qid = PL_open_query(MODULE_user, PL_Q_NODEBUG, pred, argv);
  214.       rval = PL_next_solution(qid);
  215.       PL_close_query(qid);
  216.       PL_discard_foreign_frame(cid);
  217.     } else
  218.       rval = FALSE;
  219.  
  220.     if ( !rval )
  221.       warning("%s:%d: Syntax error: %s",
  222.           stringAtom(source_file_name), source_line_no, what);
  223.   }
  224. }
  225.  
  226.  
  227. static void
  228. singletonWarning(atom_t *vars, int nvars)
  229. { fid_t cid = PL_open_foreign_frame();
  230.   qid_t qid;
  231.   term_t argv      = PL_new_term_refs(3);
  232.   term_t a         = PL_new_term_ref();
  233.   term_t h       = PL_new_term_ref();
  234.   predicate_t pred = PL_pred(FUNCTOR_exception3, MODULE_user);
  235.   int n, rval;
  236.     
  237.   PL_put_atom(    argv+0, ATOM_singleton);
  238.   PL_put_functor( argv+1, FUNCTOR_singleton3);
  239.   PL_put_variable(argv+2);
  240.   PL_get_arg(1, argv+1, a); PL_unify_atom(a, source_file_name);
  241.   PL_get_arg(2, argv+1, a); PL_unify_integer(a, source_line_no);
  242.   PL_get_arg(3, argv+1, a);
  243.            
  244.   for(n=0; n<nvars; n++)
  245.   { PL_unify_list(a, h, a);
  246.     PL_unify_atom(h, vars[n]);
  247.   }
  248.   PL_unify_nil(a);
  249.  
  250.   qid = PL_open_query(MODULE_user, PL_Q_NODEBUG, pred, argv);
  251.   rval = PL_next_solution(qid);
  252.   PL_close_query(qid);
  253.   PL_discard_foreign_frame(cid);
  254.  
  255.   if ( !rval )
  256.   { char buf[LINESIZ];
  257.  
  258.     buf[0] = EOS;
  259.     for(n=0; n<nvars; n++)
  260.     { if ( n > 0 )
  261.     strcat(buf, ", ");
  262.       strcat(buf, stringAtom(vars[n]));
  263.     }
  264.  
  265.     warning("Singleton variables: %s", buf);
  266.   }
  267. }
  268.  
  269.  
  270.         /********************************
  271.         *           RAW READING         *
  272.         *********************************/
  273.  
  274.  
  275. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  276. Scan the input, give prompts when necessary and return a char *  holding
  277. a  stripped  version of the next term.  Contigeous white space is mapped
  278. on a single space, block and % ... \n comment  is  deleted.   Memory  is
  279. claimed automatically en enlarged if necessary.
  280.  
  281. Earlier versions used to local stack for   building the term.  This does
  282. not work with O_PCE as we might be  called back via the notifier while
  283. reading.
  284.  
  285. (char *) NULL is returned on a syntax error.
  286. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  287.  
  288. static void
  289. clearBuffer()
  290. { if (rb.size == 0)
  291.   { if ( !(rb.base = (unsigned char *)malloc(RBSIZE)) )
  292.       fatalError("%s", OsError());
  293.     rb.size = RBSIZE;
  294.   }
  295.   SECURE( if ( rb.base == 0 ) fatalError("read/1: nesting=%d, size=%d",
  296.                         read_nesting, rb.size) );
  297.   rb.left = rb.size;
  298.   base = rb.here = rb.base;
  299.   DEBUG(8, Sdprintf("Cleared read buffer.rb at %ld, base at %ld\n",
  300.           (long) &rb, (long) rb.base));
  301. }      
  302.  
  303.  
  304. static inline void
  305. addToBuffer(int c)
  306. { if (rb.left-- == 0)
  307.   { if ( !(rb.base = (unsigned char *)realloc(rb.base, rb.size * 2)) )
  308.       fatalError("%s", OsError());
  309.     DEBUG(8, Sdprintf("Reallocated read buffer at %ld\n", (long) rb.base));
  310.     base = rb.base;
  311.     rb.here = rb.base + rb.size;
  312.     rb.left = rb.size - 1;
  313.     rb.size *= 2;
  314.   }
  315.   *rb.here++ = c & 0xff;
  316. }
  317.  
  318.  
  319. #define getchr() Sgetc(ioi)
  320.  
  321. #define ensure_space(c) { if ( something_read && \
  322.                    (c == '\n'|| !isBlank(rb.here[-1])) ) \
  323.                addToBuffer(c); \
  324.                 }
  325. #define set_start_line { if ( !something_read ) \
  326.              { setCurrentSourceLocation(); \
  327.                something_read++; \
  328.              } \
  329.                }
  330.  
  331. #define rawSyntaxError(what) { addToBuffer(EOS); \
  332.                    base = rb.base, token_start = rb.here-1; \
  333.                    syntaxError(what); \
  334.                  }
  335.  
  336. static char *
  337. raw_read2()
  338. { int c;
  339.   bool something_read = FALSE;
  340.   int newlines;
  341.   bool dotseen = FALSE;
  342.   IOSTREAM *ioi = PL_current_input();
  343.  
  344.   if ( !ioi )
  345.   { c = EOF;
  346.     goto handle_c;
  347.   }
  348.   
  349.   clearBuffer();                /* clear input buffer */
  350.   source_line_no = -1;
  351.  
  352.   for(;;)
  353.   { c = getchr();
  354.  
  355.   handle_c:
  356.     switch(c)
  357.     { case EOF:
  358.         if (seeingString())        /* do not require '. ' when */
  359.         { addToBuffer(' ');        /* reading from a string */
  360.           addToBuffer('.');
  361.           addToBuffer(' ');
  362.           addToBuffer(EOS);
  363.           return rb.base;
  364.         }
  365.         if (something_read)
  366.         { if ( dotseen )        /* term.<EOF> */
  367.           { if ( rb.here - rb.base == 1 )
  368.               rawSyntaxError("Unexpected end of clause");
  369.             ensure_space(' ');
  370.             addToBuffer(EOS);
  371.             return rb.base;
  372.           }
  373.           rawSyntaxError("Unexpected end of file");
  374.         }
  375.         if ( Sfpasteof(ioi) )
  376.         { warning("Attempt to read past end-of-file");
  377.           return NULL;
  378.         }
  379.         set_start_line;
  380.         strcpy(rb.base, "end_of_file. ");
  381.         return rb.base;
  382.       case '/': c = getchr();
  383.         if ( c == '*' )
  384.         { int last;
  385.           int level = 1;
  386.  
  387.           if ((last = getchr()) == EOF)
  388.             rawSyntaxError("End of file in ``/* ... */'' comment");
  389.  
  390.           if ( something_read )
  391.           { addToBuffer(' ');    /* positions */
  392.             addToBuffer(' ');
  393.             addToBuffer(last == '\n' ? last : ' ');
  394.           }
  395.  
  396.           for(;;)
  397.           { switch(c = getchr())
  398.             { case EOF:
  399.             rawSyntaxError("End of file in ``/* ... */'' comment");
  400.               case '*':
  401.             if ( last == '/' )
  402.               level++;
  403.             break;
  404.               case '/':
  405.             if ( last == '*' && --level == 0 )
  406.             { c = ' ';
  407.               goto handle_c;
  408.             }
  409.             break;
  410.             }
  411.             if ( something_read )
  412.               addToBuffer(c == '\n' ? c : ' ');
  413.             last = c;
  414.           }
  415.         } else
  416.         { addToBuffer('/');
  417.           if ( isSymbol(c) )
  418.           { while( c != EOF && isSymbol(c) )
  419.             { addToBuffer(c);
  420.               c = getchr();
  421.             }
  422.           }
  423.           dotseen = FALSE;
  424.           goto handle_c;
  425.         }
  426.       case '%': if ( something_read )
  427.           addToBuffer(' ');    /* positions */
  428.         while((c=getchr()) != EOF && c != '\n')
  429.         { if ( something_read )    /* record positions */
  430.             addToBuffer(' ');
  431.         }
  432.         c = '\n';
  433.         goto handle_c;
  434.      case '\'': if ( rb.here > rb.base && isDigit(rb.here[-1]) )
  435.         { addToBuffer(c);            /* <n>' */
  436.           if ( rb.here[-2] == '0' )        /* 0'<c> */
  437.           { if ( (c=getchr()) != EOF )
  438.             { addToBuffer(c);
  439.               break;
  440.             }
  441.             rawSyntaxError("Unexpected end of file");
  442.           }
  443.           dotseen = FALSE;
  444.           break;
  445.         }
  446.  
  447.         set_start_line;
  448.         newlines = 0;
  449.         addToBuffer(c);
  450.         while((c=getchr()) != EOF && c != '\'')
  451.         { if ( c == '\\' && CHARESCAPE )
  452.           { addToBuffer(c);
  453.             if ( (c = getchr()) == EOF )
  454.               goto eofinquoted;
  455.           } else if (c == '\n' &&
  456.                  newlines++ > MAXNEWLINES &&
  457.                  (debugstatus.styleCheck & LONGATOM_CHECK))
  458.             rawSyntaxError("Atom too long");
  459.  
  460.           addToBuffer(c);
  461.         }
  462.         if (c == EOF)
  463.         { eofinquoted:
  464.           rawSyntaxError("End of file in quoted atom");
  465.         }
  466.         addToBuffer(c);
  467.         dotseen = FALSE;
  468.         break;
  469.       case '"':    set_start_line;
  470.         newlines = 0;
  471.         addToBuffer(c);
  472.         while((c=getchr()) != EOF && c != '"')
  473.         { if ( c == '\\' && CHARESCAPE )
  474.           { addToBuffer(c);
  475.             if ( (c = getchr()) == EOF )
  476.               goto eofinstr;
  477.           } else if (c == '\n' &&
  478.                  newlines++ > MAXNEWLINES &&
  479.                  (debugstatus.styleCheck & LONGATOM_CHECK))
  480.             rawSyntaxError("String too long");
  481.           addToBuffer(c);
  482.         }
  483.         if (c == EOF)
  484.         { eofinstr:
  485.           rawSyntaxError("End of file in string");
  486.         }
  487.         addToBuffer(c);
  488.         dotseen = FALSE;
  489.         break;
  490.       case '.': addToBuffer(c);
  491.         set_start_line;
  492.         dotseen++;
  493.         c = getchr();
  494.         if ( isSymbol(c) )
  495.         { while( c != EOF && isSymbol(c) )
  496.           { addToBuffer(c);
  497.             c = getchr();
  498.           }
  499.           dotseen = FALSE;
  500.         }
  501.         goto handle_c;
  502.       default:    switch(_PL_char_types[c])
  503.         { case SP:
  504.             if ( dotseen )
  505.             { if ( rb.here - rb.base == 1 )
  506.             rawSyntaxError("Unexpected end of file");
  507.               ensure_space(c);
  508.               addToBuffer(EOS);
  509.               return rb.base;
  510.             }
  511.             do
  512.             { if ( something_read )
  513.             addToBuffer(c == '\n' ? c : ' '); /* positions */
  514.               else
  515.             ensure_space(c);
  516.               c = getchr();
  517.             } while( c != EOF && isBlank(c) );
  518.             goto handle_c;
  519.           case SY:
  520.             set_start_line;
  521.             do
  522.             { addToBuffer(c);
  523.               c = getchr();
  524.             } while( c != EOF && isSymbol(c) == SY );
  525.             dotseen = FALSE;
  526.             goto handle_c;
  527.           case LC:
  528.           case UC:
  529.             set_start_line;
  530.             do
  531.             { addToBuffer(c);
  532.               c = getchr();
  533.             } while( c != EOF && isAlpha(c) );
  534.             dotseen = FALSE;
  535.             goto handle_c;
  536.           default:
  537.             addToBuffer(c);
  538.             dotseen = FALSE;
  539.             set_start_line;
  540.         }
  541.     }
  542.   }
  543. }
  544.  
  545. static char *
  546. raw_read(void)
  547. { char *s;
  548.  
  549.   startRead();
  550.   if ( Input == 0 )
  551.   { ttybuf tab;
  552.  
  553.     PushTty(&tab, TTY_SAVE);        /* make sure tty is sane */
  554.     PopTty(&ttytab);
  555.     s = raw_read2();
  556.     PopTty(&tab);
  557.   } else
  558.     s = raw_read2();
  559.   stopRead();
  560.  
  561.   return s;
  562. }
  563.  
  564.  
  565.         /*********************************
  566.         *        VARIABLE DATABASE       *
  567.         **********************************/
  568.  
  569. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  570. These functions manipulate the  variable-name  base   for  a  term. When
  571. building the term on the global stack, variables are represented using a
  572. reference to a `struct variable'. The first  part of this structure is a
  573. struct atom, so if a garbage   collection happens, the garbage collector
  574. will simply assume an atom.
  575.  
  576. The buffer `var_buffer' is a  list  if   pointers  to  a  stock of these
  577. variable structures. This list is dynamically expanded if necessary. The
  578. buffer var_name_buffer contains the actually   strings.  They are packed
  579. together to avoid memory fragmentation. This   buffer too is reallocated
  580. if necessary. In this  case,  the   pointers  of  the  existing variable
  581. structures are relocated.
  582. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  583.  
  584. #define MAX_SINGLETONS 256
  585.  
  586. static buffer     var_name_buffer;    /* stores the names */
  587. static buffer     var_buffer;        /* array of Variables */
  588. static int     var_allocated;        /* # allocated slots there */
  589. static int     var_free;        /* 1-st free slot there */
  590. static Variable     var_list;        /* linked list of vars */
  591. static Variable  var_tail;        /* tail of linked list */
  592.  
  593. static void
  594. initVarTable()
  595. { static int done = 0;
  596.  
  597.   if ( !done )
  598.   { initBuffer(&var_name_buffer);
  599.     initBuffer(&var_buffer);
  600.     done++;
  601.   }
  602.  
  603.   var_list = var_tail = NULL;
  604.   var_free = 0;
  605.   emptyBuffer(&var_name_buffer);
  606. }
  607.  
  608.  
  609. static char *
  610. save_var_name(const char *name)
  611. { int l = strlen(name);
  612.   char *nb, *ob = baseBuffer(&var_name_buffer, char);
  613.   int e = entriesBuffer(&var_name_buffer, char);
  614.  
  615.   addMultipleBuffer(&var_name_buffer, name, l+1, char);
  616.   if ( (nb = baseBuffer(&var_name_buffer, char)) != ob )
  617.   { int shift = nb - ob;
  618.     Variable v;
  619.  
  620.     for(v = var_list; v; v = v->next)
  621.       v->name += shift;
  622.   }
  623.  
  624.   return baseBuffer(&var_name_buffer, char) + e;
  625. }
  626.  
  627.                     /* use hash-key? */
  628.  
  629. static Variable
  630. isVarAtom(word w)
  631. { if ( tagex(w) == (TAG_ATOM|STG_GLOBAL) )
  632.     return baseBuffer(&var_buffer, Variable)[w>>7];
  633.   
  634.   return NULL;
  635. }
  636.  
  637.  
  638. static Variable
  639. lookupVariable(const char *name)
  640. { Variable v;
  641.  
  642.   for( v = var_list; v; v = v->next )
  643.   { if ( streq(name, v->name) )
  644.     { v->times++;
  645.       return v;
  646.     }
  647.   }
  648.        
  649.   while ( var_free >= var_allocated )
  650.   { Variable v = allocHeap(sizeof(struct variable));
  651.     addBuffer(&var_buffer, v, Variable);
  652.     v->signature = (var_allocated<<7)|TAG_ATOM|STG_GLOBAL;
  653.     var_allocated++;
  654.   }
  655.  
  656.   v = baseBuffer(&var_buffer, Variable)[var_free++];
  657.   v->next     = NULL;
  658.   v->name     = save_var_name(name);
  659.   v->times    = 1;
  660.   v->variable = 0;
  661.   if ( var_tail )
  662.   { var_tail->next = v;
  663.     var_tail = v;
  664.   } else
  665.     var_list = var_tail = v;
  666.   
  667.   return v;
  668. }
  669.  
  670.  
  671. static bool
  672. check_singletons(term_t singles)
  673. { Variable var;
  674.  
  675.   if ( singles != TRUE )        /* returns <name> = var bindings */
  676.   { term_t list = PL_copy_term_ref(singles);
  677.     term_t head = PL_new_term_ref();
  678.  
  679.     for(var = var_list; var; var=var->next)
  680.     { if ( var->times == 1 && var->name[0] != '_' )
  681.       {    if ( !PL_unify_list(list, head, list) ||
  682.          !PL_unify_term(head,
  683.                 PL_FUNCTOR, FUNCTOR_equals2,
  684.                 PL_CHARS,    var->name,
  685.                 PL_TERM,    var->variable) )
  686.       fail;
  687.       }
  688.     }
  689.  
  690.     return PL_unify_nil(list);
  691.   } else                /* just report */
  692.   { atom_t singletons[MAX_SINGLETONS];
  693.     int i = 0;
  694.  
  695.     for(var = var_list; var; var=var->next)
  696.     { if ( var->times == 1 && var->name[0] != '_' )
  697.       { if ( singles == 1 )
  698.     { if ( i < MAX_SINGLETONS )
  699.         singletons[i++] = lookupAtom(var->name);
  700.     }
  701.       }
  702.     }
  703.  
  704.     if ( i > 0 )
  705.       singletonWarning(singletons, i);
  706.  
  707.     succeed;
  708.   }
  709. }
  710.  
  711.  
  712. static bool
  713. bind_variables(term_t bindings)
  714. { Variable var;
  715.   term_t list = PL_copy_term_ref(bindings);
  716.   term_t head = PL_new_term_ref();
  717.   term_t a    = PL_new_term_ref();
  718.  
  719.   for(var = var_list; var; var=var->next)
  720.   { if ( var->name[0] != '_' )
  721.     { if ( !PL_unify_list(list, head, list) ||
  722.        !PL_unify_functor(head, FUNCTOR_equals2) ||
  723.        !PL_get_arg(1, head, a) ||
  724.        !PL_unify_atom_chars(a, var->name) ||
  725.        !PL_get_arg(2, head, a) ||
  726.        !PL_unify(a, var->variable) )
  727.     fail;
  728.     }
  729.   }
  730.  
  731.   return PL_unify_nil(list);
  732. }
  733.  
  734.  
  735.         /********************************
  736.         *           TOKENISER           *
  737.         *********************************/
  738.  
  739. #define skipSpaces    { while(isBlank(*here) ) here++; c = *here++; }
  740. #define unget_token()    { unget = TRUE; }
  741.  
  742. forwards Token    get_token(bool);
  743. forwards void    build_term(term_t term, atom_t name, int arity, term_t *args);
  744. forwards bool    complex_term(const char *end, term_t term, term_t positions);
  745. forwards bool    simple_term(bool, term_t term, bool *isname, term_t positions);
  746.  
  747. static int
  748. scan_number(char **s, int b, Number n)
  749. { int d;
  750.   unsigned long maxi = PLMAXINT/b;    /* cache? */
  751.   unsigned long t = 0;
  752.  
  753.   while((d = digitValue(b, **s)) >= 0)
  754.   { (*s)++;
  755.  
  756.     if ( t > maxi )
  757.     { real maxf = MAXREAL / (real) b - (real) b;
  758.       real tf = (real)t;
  759.  
  760.       tf = tf * (real)b + (real)d;
  761.       while((d = digitValue(b, **s)) >= 0)
  762.       { (*s)++;
  763.         if ( tf > maxf )
  764.       fail;                /* number too large */
  765.         tf = tf * (real)b + (real)d;
  766.       }
  767.       n->value.f = tf;
  768.       n->type = V_REAL;
  769.       succeed;
  770.     } else
  771.       t = t * b + d;
  772.   }  
  773.  
  774.   if ( t > PLMAXINT )
  775.   { n->value.f = (real)t;
  776.     n->type = V_REAL;
  777.     succeed;
  778.   }
  779.  
  780.   n->value.i = t;
  781.   n->type = V_INTEGER;
  782.   succeed;
  783. }
  784.  
  785.  
  786. #define NEXT(v)    do { c = (v); goto next; } while(0) 
  787. #define AddChar(c) \
  788.     do \
  789.     { if ( n >= bufsize ) \
  790.       { if ( bufsize == 0 ) \
  791.         { bufsize = 512; \
  792.           buf = malloc(bufsize); \
  793.         } else \
  794.         { bufsize *= 2; \
  795.           buf = realloc(buf, bufsize); \
  796.         } \
  797.         if ( buf == NULL ) \
  798.           fatalError("%s", OsError()); \
  799.       } \
  800.       buf[n++] = (c); \
  801.     } while(0)
  802.         
  803.  
  804. static char *
  805. get_string(unsigned char *in, unsigned char **end)
  806. { static char *buf;
  807.   static int bufsize = 0;
  808.   int n;
  809.   int quote;
  810.   char c;
  811.  
  812.   n = 0;
  813.   quote = *in++;
  814.  
  815.   for(;;)
  816.   { c = *in++;
  817.  
  818.     if ( c == quote )
  819.     { if ( *in == quote )
  820.       { in++;
  821.     NEXT(quote);
  822.       }
  823.  
  824.       break;
  825.     }
  826.     if ( c == '\\' && CHARESCAPE )
  827.     { int c2;
  828.       int base;
  829.       int xdigits;
  830.       
  831.       switch((c2 = *in++))
  832.       { case 'a':
  833.       NEXT(7);            /* 7 is ASCII BELL */
  834.     case 'b':
  835.       NEXT('\b');
  836.     case 'c':
  837.       while(isBlank(*in))
  838.         in++;
  839.       continue;
  840.     case 10:            /* linefeed */
  841.       while(isBlank(*in) && *in != 10 )
  842.         in++;
  843.       continue;
  844.     case 'f':
  845.       NEXT('\f');
  846.     case 'n':
  847.       NEXT('\n');
  848.     case 'r':
  849.       NEXT('\r');
  850.     case 't':
  851.       NEXT('\t');
  852.     case 'v':
  853.       NEXT(11);            /* 11 is ASCII Vertical Tab */
  854.     case 'x':
  855.       c2 = *in++;
  856.       if ( digitValue(16, c2) >= 0 )
  857.       { base = 16;
  858.         xdigits = 1;
  859.         goto numchar;
  860.       } else
  861.         NEXT('x');
  862.     default:
  863.       if ( c2 >= '0' && c2 <= '7' )    /* octal number */
  864.       { int chr;
  865.         int dv;
  866.  
  867.         base = 8;
  868.         xdigits = 2;
  869.  
  870.       numchar:
  871.         chr = digitValue(base, c2);
  872.         c2 = *in++;
  873.         while(xdigits-- > 0 &&
  874.           (dv = digitValue(base, c2)) >= 0 )
  875.         { chr = chr * base + dv;
  876.           c2 = *in++;
  877.         }
  878.         if ( c2 != '\\' )
  879.           in--;
  880.         NEXT(chr);
  881.       } else
  882.         NEXT(c2);
  883.       }
  884.     }
  885.  
  886.   next:
  887.     AddChar(c);
  888.   }
  889.  
  890.   AddChar(EOS);
  891.   if ( end )
  892.     *end = in;
  893.  
  894.   return buf;
  895. }  
  896.  
  897.  
  898. int
  899. get_number(const unsigned char *cin, unsigned char **end, Number value)
  900. { int negative = FALSE;
  901.   unsigned int c;
  902.   char *in = (char *)cin;        /* const hack */
  903.  
  904.   if ( *in == '-' )            /* skip optional sign */
  905.   { negative = TRUE;
  906.     in++;
  907.   } else if ( *in == '+' )
  908.     in++;
  909.  
  910.   if ( *in == '0' )
  911.   { int base = 0;
  912.  
  913.     switch(in[1])
  914.     { case '\'':            /* 0'<char> */
  915.       { if ( isAlpha(in[3]) )
  916.       fail;                /* illegal number */
  917.     value->value.i = (long)in[2] & 0xff;
  918.     if ( negative )            /* -0'a is a bit dubious! */
  919.       value->value.i = -value->value.i;
  920.     *end = in+3;
  921.     value->type = V_INTEGER;
  922.     succeed;
  923.       }
  924.       case 'b':
  925.     base = 2;
  926.         break;
  927.       case 'x':
  928.     base = 16;
  929.         break;
  930.       case 'o':
  931.     base = 8;
  932.         break;
  933.     }
  934.  
  935.     if ( base )                /* 0b<binary>, 0x<hex>, 0o<oct> */
  936.     { int rval;
  937.       in += 2;
  938.       rval = scan_number(&in, base, value);
  939.       *end = in;
  940.       if ( negative )
  941.      value->value.i = -value->value.i;
  942.       return rval;
  943.     }
  944.   }
  945.  
  946.   if ( !isDigit(*in) || !scan_number(&in, 10, value) )
  947.     fail;                /* too large? */
  948.  
  949.                     /* base'value number */
  950.   if ( *in == '\'' )
  951.   { in++;
  952.  
  953.     if ( !intNumber(value) || value->value.i > 36 )
  954.       fail;                /* illegal base */
  955.     if ( !scan_number(&in, (int)value->value.i, value) )
  956.       fail;                /* number too large */
  957.  
  958.     if ( isAlpha(*in) )
  959.       fail;                /* illegal number */
  960.  
  961.     if ( negative )
  962.     { if ( intNumber(value) )
  963.     value->value.i = -value->value.i;
  964.       else
  965.     value->value.f = -value->value.f;
  966.     }
  967.  
  968.     *end = in;
  969.     succeed;
  970.   }
  971.                     /* Real numbers */
  972.   if ( *in == '.' && isDigit(in[1]) )
  973.   { double n;
  974.  
  975.     if ( intNumber(value) )
  976.     { value->value.f = (double) value->value.i;
  977.       value->type = V_REAL;
  978.     }
  979.     n = 10.0, in++;
  980.     while( isDigit(c = *in) )
  981.     { in++;
  982.       value->value.f += (double)(c-'0') / n;
  983.       n *= 10.0;
  984.     }
  985.   }
  986.  
  987.   if ( *in == 'e' || *in == 'E' )
  988.   { number exponent;
  989.     bool neg_exponent;
  990.  
  991.     in++;
  992.     DEBUG(9, Sdprintf("Exponent\n"));
  993.     switch(*in)
  994.     { case '-':
  995.     in++;
  996.         neg_exponent = TRUE;
  997.     break;
  998.       case '+':
  999.     in++;
  1000.       default:
  1001.     neg_exponent = FALSE;
  1002.         break;
  1003.     }
  1004.  
  1005.     if ( !scan_number(&in, 10, &exponent) || !intNumber(&exponent) )
  1006.       fail;                /* too large exponent */
  1007.  
  1008.     if ( intNumber(value) )
  1009.     { value->value.f = (double) value->value.i;
  1010.       value->type = V_REAL;
  1011.     }
  1012.  
  1013.     value->value.f *= pow((double)10.0,
  1014.               neg_exponent ? -(double)exponent.value.i
  1015.                            : (double)exponent.value.i);
  1016.   }
  1017.  
  1018.   if ( negative )
  1019.   { if ( intNumber(value) )
  1020.       value->value.i = -value->value.i;
  1021.     else
  1022.       value->value.f = -value->value.f;
  1023.   }
  1024.  
  1025.   *end = in;
  1026.   succeed;
  1027. }
  1028.  
  1029.  
  1030. static Token
  1031. get_token(bool must_be_op)
  1032. { unsigned int c;
  1033.   unsigned char *start;
  1034.   int end;
  1035.  
  1036.   if ( unget )
  1037.   { unget = FALSE;
  1038.     return &token;
  1039.   }
  1040.  
  1041.   skipSpaces;
  1042.   token_start = here - 1;
  1043.   token.start = source_char_no + token_start - base;
  1044.   switch(_PL_char_types[c])
  1045.   { case LC:    { start = here-1;
  1046.           while(isAlpha(*here) )
  1047.             here++;
  1048.           c = *here;
  1049.           *here = EOS;
  1050.           token.value.atom = lookupAtom(start);
  1051.           *here = c;
  1052.           token.type = (c == '(' ? T_FUNCTOR : T_NAME);
  1053.           DEBUG(9, Sdprintf("%s: %s\n", c == '(' ? "FUNC" : "NAME",
  1054.                     stringAtom(token.value.atom)));
  1055.  
  1056.           break;
  1057.         }
  1058.     case UC:    { start = here-1;
  1059.           while(isAlpha(*here) )
  1060.             here++;
  1061.           c = *here;
  1062.           *here = EOS;
  1063.           if ( c == '(' && trueFeature(ALLOW_VARNAME_FUNCTOR) )
  1064.           { token.value.atom = lookupAtom(start);
  1065.             token.type = T_FUNCTOR;
  1066.             *here = c;
  1067.             break;
  1068.           }
  1069.           if (start[0] == '_' && here == start + 1)
  1070.           { DEBUG(9, Sdprintf("VOID\n"));
  1071.             token.type = T_VOID;
  1072.           } else
  1073.           { token.value.variable = lookupVariable(start);
  1074.             DEBUG(9, Sdprintf("VAR: %s\n",
  1075.                       token.value.variable->name));
  1076.             token.type = T_VARIABLE;
  1077.           }
  1078.           *here = c;
  1079.  
  1080.           break;
  1081.         }
  1082.     case_digit:
  1083.     case DI:    { number value;
  1084.  
  1085.           if ( get_number(&here[-1], &here, &value) &&
  1086.                !isAlpha(here[0]) )
  1087.           { token.value.number = value;
  1088.             token.type = T_NUMBER;
  1089.             break;
  1090.           } else
  1091.             syntaxError("Illegal number");
  1092.         }
  1093.     case SO:    { char tmp[2];
  1094.  
  1095.           tmp[0] = c, tmp[1] = EOS;
  1096.           token.value.atom = lookupAtom(tmp);
  1097.           token.type = (*here == '(' ? T_FUNCTOR : T_NAME);
  1098.           DEBUG(9, Sdprintf("%s: %s\n",
  1099.                   *here == '(' ? "FUNC" : "NAME",
  1100.                   stringAtom(token.value.atom)));
  1101.  
  1102.           break;
  1103.         }
  1104.     case SY:    { start = here - 1;
  1105.           while( isSymbol(*here) )
  1106.             here++;
  1107.  
  1108.           if ( here == start+1 )
  1109.           { if ( (c == '+' || c == '-') &&    /* +- number */
  1110.              !must_be_op &&
  1111.              isDigit(*here) )
  1112.             { goto case_digit;
  1113.             }
  1114.             if ( c == '.' && isBlank(*here) )    /* .<blank> */
  1115.             { token.type = T_FULLSTOP;
  1116.               break;
  1117.             }
  1118.           }
  1119.  
  1120.           end = *here, *here = EOS;
  1121.           token.value.atom = lookupAtom((char *)start);
  1122.           *here = end;
  1123.  
  1124.           token.type = (end == '(' ? T_FUNCTOR : T_NAME);
  1125.           DEBUG(9, Sdprintf("%s: %s\n",
  1126.                     end == '(' ? "FUNC" : "NAME",
  1127.                     stringAtom(token.value.atom)));
  1128.  
  1129.           break;
  1130.         }
  1131.     case PU:    { switch(c)
  1132.           { case '{':
  1133.             case '[':
  1134.               while( isBlank(*here) )
  1135.             here++;
  1136.               if (here[0] == matchingBracket(c))
  1137.               { here++;
  1138.             token.value.atom = (c == '[' ? ATOM_nil : ATOM_curl);
  1139.             token.type = here[0] == '(' ? T_FUNCTOR : T_NAME;
  1140.             DEBUG(9, Sdprintf("NAME: %s\n",
  1141.                       stringAtom(token.value.atom)));
  1142.             goto out;
  1143.               }
  1144.           }
  1145.           token.value.character = c;
  1146.           token.type = T_PUNCTUATION;
  1147.           DEBUG(9, Sdprintf("PUNCT: %c\n", token.value.character));
  1148.  
  1149.           break;
  1150.         }
  1151.     case SQ:    { char *s = get_string(here-1, &here);
  1152.  
  1153.           token.value.atom = lookupAtom(s);
  1154.           token.type = (here[0] == '(' ? T_FUNCTOR : T_NAME);
  1155.           break;
  1156.         }
  1157.     case DQ:    { char *s = get_string(here-1, &here);
  1158.           term_t t = PL_new_term_ref();
  1159.  
  1160. #if O_STRING
  1161.            if ( debugstatus.styleCheck & STRING_STYLE )
  1162.             PL_put_string_chars(t, s);
  1163.           else
  1164.             PL_put_list_chars(t, s);
  1165. #else
  1166.           PL_put_list_chars(t, s);
  1167. #endif /* O_STRING */
  1168.             token.value.term = t;
  1169.           token.type = T_STRING;
  1170.           break;
  1171.         }
  1172.     default:    { sysError("read/1: tokeniser internal error");
  1173.               break;        /* make lint happy */
  1174.         }
  1175.   }
  1176.  
  1177. out:
  1178.   token.end = source_char_no + here - base;
  1179.  
  1180.   return &token;
  1181. }
  1182.  
  1183.          /*******************************
  1184.          *       TERM-BUILDING    *
  1185.          *******************************/
  1186.  
  1187. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1188. Build a term on the global stack,  given   the  atom of the functor, the
  1189. arity and a vector of  arguments.   The  argument vector either contains
  1190. nonvar terms or a variable block. The latter looks like an atom (to make
  1191. a possible invocation of the   garbage-collector or stack-shifter happy)
  1192. and is recognised by  the  value   of  its  character-pointer,  which is
  1193. statically allocated and thus unique.
  1194. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  1195.  
  1196. #define setHandle(h, w)        (*valTermRef(h) = (w))
  1197.  
  1198. static inline void
  1199. readValHandle(term_t term, Word argp)
  1200. { word w = *valTermRef(term);
  1201.   Variable var;
  1202.  
  1203.   if ( (var = isVarAtom(w)) )
  1204.   { DEBUG(9, Sdprintf("readValHandle(): var at 0x%x\n", var));
  1205.  
  1206.     if ( !var->variable )        /* new variable */
  1207.     { var->variable = PL_new_term_ref();
  1208.       setVar(*argp);
  1209.       *valTermRef(var->variable) = makeRef(argp);
  1210.     } else                /* reference to existing var */
  1211.     { *argp = *valTermRef(var->variable);
  1212.     }
  1213.   } else
  1214.     *argp = w;                /* plain value */
  1215. }
  1216.  
  1217.  
  1218. static void
  1219. build_term(term_t term, atom_t atom, int arity, term_t *argv)
  1220. { functor_t functor = lookupFunctorDef(atom, arity);
  1221.   Word argp = allocGlobal(arity+1);
  1222.  
  1223.   DEBUG(9, Sdprintf("Building term %s/%d ... ", stringAtom(atom), arity));
  1224.   setHandle(term, consPtr(argp, TAG_COMPOUND|STG_GLOBAL));
  1225.   *argp++ = functor;
  1226.  
  1227.   for( ; arity-- > 0; argv++, argp++)
  1228.     readValHandle(*argv, argp);
  1229.  
  1230.   DEBUG(9, Sdprintf("result: "); pl_write(term); Sdprintf("\n") );
  1231. }
  1232.  
  1233.  
  1234. static void
  1235. PL_assign_term(term_t to, term_t from)
  1236. { *valTermRef(to) = *valTermRef(from);
  1237. }
  1238.  
  1239.  
  1240.         /********************************
  1241.         *             PARSER            *
  1242.         *********************************/
  1243.  
  1244. #define priorityClash { syntaxError("Operator priority clash"); }
  1245.  
  1246. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1247. This part of the parser actually constructs  the  term.   It  calls  the
  1248. tokeniser  to  find  the next token and assumes the tokeniser implements
  1249. one-token pushback efficiently.  It consists  of  two  mutual  recursive
  1250. functions:  complex_term()  which is involved with operator handling and
  1251. simple_term() which reads everything, except for operators.
  1252. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  1253.  
  1254. typedef struct
  1255. { atom_t op;                /* Name of the operator */
  1256.   short    kind;                /* kind (prefix/postfix/infix) */
  1257.   short    left_pri;            /* priority at left-hand */
  1258.   short    right_pri;            /* priority at right hand */
  1259.   short    op_pri;                /* priority of operator */
  1260.   term_t tpos;                /* term-position */
  1261.   unsigned char *token_start;        /* start of the token for message */
  1262. } op_entry;
  1263.  
  1264.  
  1265. typedef struct
  1266. { term_t term;                /* the term */
  1267.   term_t tpos;                /* its term-position */
  1268.   int     pri;                /* priority of the term */
  1269. } out_entry;
  1270.  
  1271.  
  1272. static bool
  1273. isOp(atom_t atom, int kind, op_entry *e)
  1274. { Operator op = isCurrentOperator(atom, kind);
  1275.   int pri;
  1276.  
  1277.   if ( op == NULL )
  1278.     fail;
  1279.   e->op     = atom;
  1280.   e->kind   = kind;
  1281.   e->op_pri = pri = op->priority;
  1282.  
  1283.   switch(op->type)
  1284.   { case OP_FX:        e->left_pri = 0;     e->right_pri = pri-1; break;
  1285.     case OP_FY:        e->left_pri = 0;     e->right_pri = pri;   break;
  1286.     case OP_XF:        e->left_pri = pri-1; e->right_pri = 0;     break;
  1287.     case OP_YF:        e->left_pri = pri;   e->right_pri = 0;     break;
  1288.     case OP_XFX:    e->left_pri = pri-1; e->right_pri = pri-1; break;
  1289.     case OP_XFY:    e->left_pri = pri-1; e->right_pri = pri;   break;
  1290.     case OP_YFX:    e->left_pri = pri;   e->right_pri = pri-1; break;
  1291.     case OP_YFY:    e->left_pri = pri;   e->right_pri = pri;   break;
  1292.   }
  1293.  
  1294.   succeed;
  1295. }
  1296.  
  1297. static void
  1298. build_op_term(term_t term, atom_t atom, int arity, out_entry *argv)
  1299. { term_t av[2];
  1300.  
  1301.   av[0] = argv[0].term;
  1302.   av[1] = argv[1].term;
  1303.   build_term(term, atom, arity, av);
  1304. }
  1305.  
  1306. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1307. realloca() maintains a buffer using  alloca()   that  has  the requested
  1308. size. The current size is maintained in   a long just below the returned
  1309. area. This is a long, to ensure   proper  allignment of the remainder of
  1310. the values.
  1311. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  1312.  
  1313. #define realloca(p, unit, n) \
  1314.     { long *ip = (long *)(p); \
  1315.       if ( ip == NULL || ip[-1] < (n) ) \
  1316.       { long nsize = (((n)+(n)/2) + 3) & ~3; \
  1317.         long *np = alloca(nsize * unit + sizeof(long)); \
  1318.         *np++ = nsize; \
  1319.         if ( ip ) \
  1320.           memcpy(np, ip, ip[-1] * unit); \
  1321.         p = (void *)np; \
  1322.       } \
  1323.     }
  1324.  
  1325. #define PushOp() \
  1326.     realloca(side, sizeof(*side), side_n+1); \
  1327.     side[side_n++] = in_op; \
  1328.     side_p = (side_n == 1 ? 0 : side_p+1);
  1329.     
  1330. #define Modify(cpri) \
  1331.     if ( side_p >= 0 && cpri > side[side_p].right_pri ) \
  1332.     { term_t tmp; \
  1333.       if ( side[side_p].kind == OP_PREFIX && rmo == 0 ) \
  1334.       { DEBUG(9, Sdprintf("Prefix %s to atom\n", \
  1335.                   stringAtom(side[side_p].op))); \
  1336.         rmo++; \
  1337.         tmp = PL_new_term_ref(); \
  1338.         PL_put_atom(tmp, side[side_p].op); \
  1339.         realloca(out, sizeof(*out), out_n+1); \
  1340.         out[out_n].term = tmp; \
  1341.         out[out_n].tpos = side[side_p].tpos; \
  1342.         out[out_n].pri = 0; \
  1343.         out_n++; \
  1344.         side_n--; \
  1345.         side_p = (side_n == 0 ? -1 : side_p-1); \
  1346.       } else if ( side[side_p].kind == OP_INFIX && out_n > 0 && rmo == 0 && \
  1347.               isOp(side[side_p].op, OP_POSTFIX, &side[side_p]) ) \
  1348.       { DEBUG(9, Sdprintf("Infix %s to postfix\n", \
  1349.                   stringAtom(side[side_p].op))); \
  1350.         rmo++; \
  1351.         tmp = PL_new_term_ref(); \
  1352.         build_op_term(tmp, side[side_p].op, 1, &out[out_n-1]); \
  1353.         out[out_n-1].pri  = side[side_p].op_pri; \
  1354.         out[out_n-1].term = tmp; \
  1355.         side[side_p].kind = OP_POSTFIX; \
  1356.         out[out_n-1].tpos = opPos(&side[side_p], &out[out_n-1]); \
  1357.         side_n--; \
  1358.         side_p = (side_n == 0 ? -1 : side_p-1); \
  1359.       } \
  1360.     }
  1361.  
  1362.  
  1363. static int
  1364. get_int_arg(term_t t, int n)
  1365. { Word p = valTermRef(t);
  1366.  
  1367.   deRef(p);
  1368.  
  1369.   return valInt(argTerm(*p, n-1));
  1370. }
  1371.  
  1372.  
  1373. static term_t
  1374. opPos(op_entry *op, out_entry *args)
  1375. { if ( op->tpos )
  1376.   { int fs = get_int_arg(op->tpos, 1);
  1377.     int fe = get_int_arg(op->tpos, 2);
  1378.     term_t r = PL_new_term_ref();
  1379.  
  1380.     if ( op->kind == OP_INFIX )
  1381.     { int s = get_int_arg(args[0].tpos, 1);
  1382.       int e = get_int_arg(args[1].tpos, 2);
  1383.  
  1384.       PL_unify_term(r,
  1385.             PL_FUNCTOR,    FUNCTOR_term_position5,
  1386.             PL_INTEGER,    s,
  1387.             PL_INTEGER,    e,
  1388.             PL_INTEGER, fs,
  1389.             PL_INTEGER, fe,
  1390.             PL_LIST,    2, PL_TERM, args[0].tpos,
  1391.                        PL_TERM, args[1].tpos);
  1392.     } else
  1393.     { int s, e;
  1394.       
  1395.       if ( op->kind == OP_PREFIX )
  1396.       { s = fs;
  1397.     e = get_int_arg(args[0].tpos, 2);
  1398.       } else
  1399.       { s = get_int_arg(args[0].tpos, 1);
  1400.     e = fe;
  1401.       }
  1402.  
  1403.       PL_unify_term(r,
  1404.             PL_FUNCTOR,    FUNCTOR_term_position5,
  1405.             PL_INTEGER,    s,
  1406.             PL_INTEGER,    e,
  1407.             PL_INTEGER, fs,
  1408.             PL_INTEGER, fe,
  1409.             PL_LIST,    1, PL_TERM, args[0].tpos);
  1410.     }
  1411.     
  1412.     return r;
  1413.   }
  1414.  
  1415.   return 0;
  1416. }
  1417.  
  1418.  
  1419. static int
  1420. can_reduce(out_entry *out, op_entry *op)
  1421. { int rval;
  1422.  
  1423.   switch(op->kind)
  1424.   { case OP_PREFIX:
  1425.       rval = op->right_pri >= out[0].pri;
  1426.       break;
  1427.     case OP_POSTFIX:
  1428.       rval = op->left_pri >= out[0].pri;
  1429.       break;
  1430.     case OP_INFIX:
  1431.       rval = op->left_pri >= out[0].pri &&
  1432.          op->right_pri >= out[1].pri;
  1433.       break;
  1434.     default:
  1435.       assert(0);
  1436.       rval = FALSE;
  1437.   }
  1438.  
  1439.   return rval;
  1440. }
  1441.  
  1442. static int
  1443. bad_operator(out_entry *out, op_entry *op)
  1444. { char buf[1024];
  1445.   term_t t;
  1446.   atom_t name;
  1447.   int arity;
  1448.   char *opname = stringAtom(op->op);
  1449.  
  1450.   token_start = op->token_start;
  1451.  
  1452.   switch(op->kind)
  1453.   { case OP_INFIX:
  1454.       if ( op->left_pri < out[0].pri )
  1455.     t = out[0].term;
  1456.       else
  1457.       { token_start += strlen(opname);
  1458.     t = out[1].term;
  1459.       }
  1460.       break;
  1461.     case OP_PREFIX:
  1462.       token_start += strlen(opname);
  1463.       /*FALL THROUGH*/
  1464.     default:
  1465.       t = out[0].term;
  1466.   }
  1467.  
  1468.   if ( PL_get_name_arity(t, &name, &arity) )
  1469.   { Ssprintf(buf, "Operator `%s' conflicts with `%s'",
  1470.          opname, stringAtom(name));
  1471.   } else
  1472.   { Ssprintf(buf, "Unknown conflict with operator `%s'", opname);
  1473.   }
  1474.  
  1475.   syntaxError(buf);
  1476. }
  1477.  
  1478. #define Reduce(cpri) \
  1479.     while( out_n > 0 && side_p >= 0 && (cpri) >= side[side_p].op_pri ) \
  1480.     { int arity = (side[side_p].kind == OP_INFIX ? 2 : 1); \
  1481.       term_t tmp; \
  1482.       if ( arity > out_n ) break; \
  1483.       if ( !can_reduce(&out[out_n-arity], &side[side_p]) ) \
  1484.           { if ( (cpri) == (OP_MAXPRIORITY+1) ) \
  1485.               return bad_operator(&out[out_n-arity], &side[side_p]); \
  1486.         break; \
  1487.       } \
  1488.       DEBUG(9, Sdprintf("Reducing %s/%d\n", \
  1489.                 stringAtom(side[side_p].op), arity));\
  1490.       tmp = PL_new_term_ref(); \
  1491.       out_n -= arity; \
  1492.       build_op_term(tmp, side[side_p].op, arity, &out[out_n]); \
  1493.       out[out_n].pri  = side[side_p].op_pri; \
  1494.       out[out_n].term = tmp; \
  1495.       out[out_n].tpos = opPos(&side[side_p], &out[out_n]); \
  1496.       out_n ++; \
  1497.       side_n--; \
  1498.       side_p = (side_n == 0 ? -1 : side_p-1); \
  1499.     }
  1500.  
  1501.  
  1502. static bool
  1503. complex_term(const char *stop, term_t term, term_t positions)
  1504. { out_entry *out  = NULL;
  1505.   op_entry  *side = NULL;
  1506.   op_entry  in_op;
  1507.   int out_n = 0, side_n = 0;
  1508.   int rmo = 0;                /* Rands more than operators */
  1509.   int side_p = -1;
  1510.   term_t pin;
  1511.  
  1512.   for(;;)
  1513.   { bool isname;
  1514.     Token token;
  1515.     term_t in = PL_new_term_ref();
  1516.  
  1517.     if ( positions )
  1518.       pin = PL_new_term_ref();
  1519.     else
  1520.       pin = 0;
  1521.   
  1522.     if ( out_n != 0 || side_n != 0 )    /* Check for end of term */
  1523.     { if ( (token = get_token(rmo == 1)) == (Token) NULL )
  1524.     fail;
  1525.       unget_token();            /* only look-ahead! */
  1526.  
  1527.       switch(token->type)
  1528.       { case T_FULLSTOP:
  1529.       if ( stop == NULL )
  1530.         goto exit;
  1531.       break;
  1532.     case T_PUNCTUATION:
  1533.     { if ( stop != NULL && strchr(stop, token->value.character) )
  1534.         goto exit;
  1535.     }
  1536.       }
  1537.     }
  1538.  
  1539.                     /* Read `simple' term */
  1540.     TRY( simple_term(rmo == 1, in, &isname, pin) );
  1541.  
  1542.     if ( isname )            /* Check for operators */
  1543.     { atom_t name;
  1544.  
  1545.       PL_get_atom(in, &name);
  1546.       in_op.tpos = pin;
  1547.       in_op.token_start = token_start;
  1548.  
  1549.       DEBUG(9, Sdprintf("name %s, rmo = %d\n", stringAtom(name), rmo));
  1550.  
  1551.       if ( isOp(name, OP_INFIX, &in_op) )
  1552.       { DEBUG(9, Sdprintf("Infix op: %s\n", stringAtom(name)));
  1553.  
  1554.     Modify(in_op.left_pri);
  1555.     if ( rmo == 1 )
  1556.     { Reduce(in_op.left_pri);
  1557.       PushOp();
  1558.       rmo--;
  1559.  
  1560.       continue;
  1561.     }
  1562.       }
  1563.       if ( isOp(name, OP_POSTFIX, &in_op) )
  1564.       { DEBUG(9, Sdprintf("Postfix op: %s\n", stringAtom(name)));
  1565.  
  1566.     Modify(in_op.left_pri);
  1567.     if ( rmo == 1 )
  1568.     { Reduce(in_op.left_pri);
  1569.       PushOp();    
  1570.     
  1571.       continue;
  1572.     }
  1573.       }
  1574.       if ( rmo == 0 && isOp(name, OP_PREFIX, &in_op) )
  1575.       { DEBUG(9, Sdprintf("Prefix op: %s\n", stringAtom(name)));
  1576.     
  1577.     PushOp();
  1578.  
  1579.     continue;
  1580.       }
  1581.     }
  1582.  
  1583.     if ( rmo != 0 )
  1584.       syntaxError("Operator expected");
  1585.     rmo++;
  1586.     realloca(out, sizeof(*out), out_n+1);
  1587.     out[out_n].pri = 0;
  1588.     out[out_n].term = in;
  1589.     out[out_n++].tpos = pin;
  1590.   }
  1591.  
  1592. exit:
  1593.   Modify(OP_MAXPRIORITY+1);
  1594.   Reduce(OP_MAXPRIORITY+1);
  1595.  
  1596.   if ( out_n == 1 && side_n == 0 )    /* simple term */
  1597.   { PL_assign_term(term, out[0].term);
  1598.     if ( positions )
  1599.       PL_unify(positions, out[0].tpos);
  1600.     succeed;
  1601.   }
  1602.  
  1603.   if ( out_n == 0 && side_n == 1 )    /* single operator */
  1604.   { PL_put_atom(term, side[0].op);
  1605.     if ( positions )
  1606.       PL_unify(positions, side[0].tpos);
  1607.     succeed;
  1608.   }
  1609.  
  1610.   syntaxError("Unbalanced operators");
  1611. }
  1612.  
  1613.  
  1614. static bool
  1615. simple_term(bool must_be_op, term_t term, bool *name, term_t positions)
  1616. { Token token;
  1617.  
  1618.   *name = FALSE;
  1619.  
  1620.   if ( !(token = get_token(must_be_op)) )
  1621.     fail;
  1622.  
  1623.   switch(token->type)
  1624.   { case T_FULLSTOP:
  1625.       syntaxError("Unexpected end of clause");
  1626.     case T_VOID:
  1627.       setHandle(term, 0L);        /* variable */
  1628.       goto atomic_out;
  1629.     case T_VARIABLE:
  1630.       setHandle(term, token->value.variable->signature);
  1631.       DEBUG(9, Sdprintf("Pushed var at 0x%x\n", token->value.variable));
  1632.       goto atomic_out;
  1633.     case T_NAME:
  1634.       *name = TRUE;
  1635.       PL_put_atom(term, token->value.atom);
  1636.       goto atomic_out;
  1637.     case T_NUMBER:
  1638.       _PL_put_number(term, &token->value.number);
  1639.     atomic_out:
  1640.       if ( positions )
  1641.       { PL_unify_term(positions,
  1642.               PL_FUNCTOR, FUNCTOR_minus2,
  1643.               PL_INTEGER, token->start,
  1644.               PL_INTEGER, token->end);
  1645.       }
  1646.       succeed;
  1647.     case T_STRING:
  1648.       PL_put_term(term,    token->value.term);
  1649.       if ( positions )
  1650.       { PL_unify_term(positions,
  1651.               PL_FUNCTOR, FUNCTOR_string_position2,
  1652.               PL_INTEGER, token->start,
  1653.               PL_INTEGER, token->end);
  1654.       }
  1655.       succeed;
  1656.     case T_FUNCTOR:
  1657.       { if ( must_be_op )
  1658.     { *name = TRUE;
  1659.       PL_put_atom(term, token->value.atom);
  1660.     } else
  1661.     { term_t av[16];
  1662.       int avn = 16;
  1663.       term_t *argv = av;
  1664.       int argc;
  1665.       atom_t functor;
  1666.       term_t pa;            /* argument */
  1667.       term_t pe;            /* term-end */
  1668.       term_t ph;
  1669.  
  1670.       if ( positions )
  1671.       { pa = PL_new_term_ref();
  1672.         pe = PL_new_term_ref();
  1673.         ph = PL_new_term_ref();
  1674.         PL_unify_term(positions,
  1675.               PL_FUNCTOR, FUNCTOR_term_position5,
  1676.               PL_INTEGER, token->start,
  1677.               PL_TERM,    pe,
  1678.               PL_INTEGER, token->start,
  1679.               PL_INTEGER, token->end,
  1680.               PL_TERM,    pa);
  1681.       } else
  1682.         pa = pe = ph = 0;
  1683.  
  1684.       functor = token->value.atom;
  1685.       argc = 0, argv;
  1686.       get_token(must_be_op); /* skip '(' */
  1687.  
  1688.       do
  1689.       { if ( argc == avn )
  1690.         { term_t *nargv = alloca(sizeof(term_t) * avn * 2);
  1691.           memcpy(nargv, argv, sizeof(term_t) * avn);
  1692.           avn *= 2;
  1693.           argv = nargv;
  1694.         }
  1695.         argv[argc] = PL_new_term_ref();
  1696.         if ( positions )
  1697.         { PL_unify_list(pa, ph, pa);
  1698.         }
  1699.         TRY( complex_term(",)", argv[argc], ph) );
  1700.         argc++;
  1701.         token = get_token(must_be_op); /* `,' or `)' */
  1702.       } while(token->value.character == ',');
  1703.  
  1704.       if ( positions )
  1705.       { PL_unify_integer(pe, token->end);
  1706.         PL_unify_nil(pa);
  1707.       }
  1708.  
  1709.       build_term(term, functor, argc, argv);
  1710.     }
  1711.     succeed;
  1712.       }
  1713.     case T_PUNCTUATION:
  1714.       { switch(token->value.character)
  1715.     { case '(':
  1716.         { int start = token->start;
  1717.  
  1718.           TRY( complex_term(")", term, positions) );
  1719.           token = get_token(must_be_op);    /* skip ')' */
  1720.  
  1721.           if ( positions )
  1722.           { Word p = argTermP(*valTermRef(positions), 0);
  1723.  
  1724.         *p++ = consInt(start);
  1725.         *p   = consInt(token->end);
  1726.           }
  1727.  
  1728.           succeed;
  1729.         }
  1730.       case '{':
  1731.         { term_t arg = PL_new_term_ref();
  1732.           term_t pa, pe;
  1733.  
  1734.           if ( positions )
  1735.           { pa = PL_new_term_ref();
  1736.         pe = PL_new_term_ref();
  1737.  
  1738.         PL_unify_term(positions,
  1739.                   PL_FUNCTOR, FUNCTOR_brace_term_position3,
  1740.                   PL_INTEGER, token->start,
  1741.                   PL_TERM,      pe,
  1742.                   PL_TERM,      pa);
  1743.           } else
  1744.         pe = pa = 0;
  1745.  
  1746.           TRY( complex_term("}", arg, pa) );
  1747.           token = get_token(must_be_op);
  1748.           if ( positions )
  1749.         PL_unify_integer(pe, token->end);
  1750.           build_term(term, ATOM_curl, 1, &arg);
  1751.  
  1752.           succeed;
  1753.         }
  1754.       case '[':
  1755.         { term_t tail = PL_new_term_ref();
  1756.           term_t tmp  = PL_new_term_ref();
  1757.           term_t pa, pe, pt, p2;
  1758.  
  1759.           if ( positions )
  1760.           { pa = PL_new_term_ref();
  1761.         pe = PL_new_term_ref();
  1762.         pt = PL_new_term_ref();
  1763.         p2 = PL_new_term_ref();
  1764.  
  1765.         PL_unify_term(positions,
  1766.                   PL_FUNCTOR, FUNCTOR_list_position4,
  1767.                   PL_INTEGER, token->start,
  1768.                   PL_TERM,    pe,
  1769.                   PL_TERM,      pa,
  1770.                   PL_TERM,      pt);
  1771.           } else
  1772.         pa = pe = p2 = pt = 0;
  1773.  
  1774. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1775. Reading a list. Tmp is used to  read   the  next element. Tail is a very
  1776. special term-ref. It is always a reference   to the place where the next
  1777. term is to be written.
  1778. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  1779.  
  1780.           PL_put_term(tail, term);
  1781.  
  1782.           for(;;)
  1783.           { Word argp;
  1784.  
  1785.         if ( positions )
  1786.           PL_unify_list(pa, p2, pa);
  1787.         TRY( complex_term(",|]", tmp, p2) );
  1788.         argp = allocGlobal(3);
  1789.         *unRef(*valTermRef(tail)) = consPtr(argp,
  1790.                             TAG_COMPOUND|STG_GLOBAL);
  1791.         *argp++ = FUNCTOR_dot2;
  1792.         readValHandle(tmp, argp++);
  1793.         setVar(*argp);
  1794.         setHandle(tail, makeRef(argp));
  1795.         
  1796.         token = get_token(must_be_op);
  1797.  
  1798.         switch(token->value.character)
  1799.         { case ']':
  1800.             { if ( positions )
  1801.               { PL_unify_nil(pa);
  1802.             PL_unify_atom(pt, ATOM_none);
  1803.             PL_unify_integer(pe, token->end);
  1804.               }
  1805.               return PL_unify_nil(tail);
  1806.             }
  1807.           case '|':
  1808.             { TRY( complex_term("]", tmp, pt) );
  1809.               argp = unRef(*valTermRef(tail));
  1810.               readValHandle(tmp, argp);
  1811.               token = get_token(must_be_op); /* discard ']' */
  1812.               if ( positions )
  1813.               { PL_unify_nil(pa);
  1814.             PL_unify_integer(pe, token->end);
  1815.               }
  1816.               succeed;
  1817.             }
  1818.           case ',':
  1819.               continue;
  1820.         }
  1821.           }
  1822.         }
  1823.       case ',':
  1824.       case '|':
  1825.       case ')':
  1826.       case '}':
  1827.       case ']':
  1828.       default:
  1829.         { static atom_t cache[256];
  1830.           int c = token->value.character;
  1831.  
  1832.           if ( !cache[c] )
  1833.           { char tmp[2];
  1834.  
  1835.         tmp[0] = c;
  1836.         tmp[1] = EOS;
  1837.         cache[c] = lookupAtom(tmp);
  1838.           }
  1839.  
  1840.           *name = TRUE; 
  1841.           PL_put_atom(term, cache[c]);
  1842.  
  1843.           succeed;
  1844.         }
  1845.     }
  1846.       } /* case T_PUNCTUATION */
  1847.     default:;
  1848.       sysError("read/1: Illegal token type (%d)", token->type);
  1849.       /*NOTREACHED*/
  1850.       fail;
  1851.   }
  1852. }
  1853.  
  1854.  
  1855. static bool
  1856. read_term(term_t term, term_t variables, term_t positions, term_t singles)
  1857. { Token token;
  1858.   term_t result;
  1859.   Word p;
  1860.  
  1861.   if ( !(base = raw_read()) )
  1862.     fail;
  1863.  
  1864.   initVarTable();
  1865.   here = base;
  1866.   unget = FALSE;
  1867.  
  1868.   result = PL_new_term_ref();
  1869.   TRY(complex_term(NULL, result, positions));
  1870.   p = valTermRef(result);
  1871.   if ( isVarAtom(*p) )            /* reading a single variable */
  1872.     readValHandle(result, p);
  1873.  
  1874.   if ( !(token = get_token(FALSE)) )
  1875.     fail;
  1876.   if (token->type != T_FULLSTOP)
  1877.     syntaxError("End of clause expected");
  1878.  
  1879.   TRY(PL_unify(term, result));
  1880.   if ( variables )
  1881.     TRY(bind_variables(variables));
  1882.   if ( singles )
  1883.     TRY(check_singletons(singles));
  1884.  
  1885.   succeed;
  1886. }
  1887.  
  1888.         /********************************
  1889.         *       PROLOG CONNECTION       *
  1890.         *********************************/
  1891.  
  1892. word
  1893. pl_raw_read(term_t term)
  1894. { char *s, *top;
  1895.  
  1896.   s = raw_read();
  1897.  
  1898.   if ( s == (char *) NULL )
  1899.     fail;
  1900.   
  1901.                     /* strip the input from blanks */
  1902.   for(top = s+strlen(s)-1; top > s && isBlank(*top); top--);
  1903.   if ( *top == '.' )
  1904.   { for(top--; top > s && isBlank(*top); top--)
  1905.       ;
  1906.   }
  1907.   top[1] = EOS;
  1908.  
  1909.   for(; isBlank(*s); s++);
  1910.  
  1911.   return PL_unify_atom_chars(term, s);
  1912. }
  1913.  
  1914.  
  1915. word
  1916. pl_raw_read2(term_t stream, term_t term)
  1917. { streamInput(stream, pl_raw_read(term));
  1918. }
  1919.  
  1920.  
  1921. word
  1922. pl_read_variables(term_t term, term_t variables)
  1923. { return read_term(term, variables, 0, FALSE);
  1924. }
  1925.  
  1926. word
  1927. pl_read_variables3(term_t stream, term_t term, term_t variables)
  1928. { streamInput(stream, pl_read_variables(term, variables));
  1929. }
  1930.  
  1931. word
  1932. pl_read(term_t term)
  1933. { return read_term(term, 0, 0, FALSE);
  1934. }
  1935.  
  1936. word
  1937. pl_read2(term_t stream, term_t term)
  1938. { streamInput(stream, pl_read(term));
  1939. }
  1940.  
  1941. word
  1942. pl_read_clause(term_t term)
  1943. { return read_term(term, 0, 0,
  1944.            debugstatus.styleCheck & SINGLETON_CHECK ? TRUE : FALSE);
  1945. }
  1946.  
  1947. word
  1948. pl_read_clause2(term_t stream, term_t term)
  1949. { streamInput(stream, pl_read_clause(term));
  1950. }
  1951.  
  1952. static const opt_spec read_term_options[] = 
  1953. { { ATOM_syntax_errors,        OPT_TERM },    /* quiet, fail, X */
  1954.   { ATOM_variable_names,    OPT_TERM },
  1955.   { ATOM_singletons,        OPT_TERM },
  1956.   { ATOM_term_position,     OPT_TERM },
  1957.   { ATOM_subterm_positions, OPT_TERM },
  1958.   { NULL_ATOM,                 0 }
  1959. };
  1960.  
  1961. word
  1962. pl_read_term(term_t term, term_t options)
  1963. { term_t syntax_errors = 0;        /* default */
  1964.   term_t varnames      = 0;
  1965.   term_t singles       = 0;
  1966.   term_t tpos           = 0;
  1967.   term_t subtpos       = 0;
  1968.   int osyntax            = give_syntaxerrors;
  1969.   int rval;
  1970.   atom_t w;
  1971.  
  1972.   if ( !scan_options(options, 0, ATOM_read_option, read_term_options,
  1973.              &syntax_errors, &varnames, &singles, &tpos, &subtpos) )
  1974.     return warning("read_term/2: illegal option list");
  1975.  
  1976.   if ( !syntax_errors ||
  1977.        (PL_get_atom(syntax_errors, &w) && w != ATOM_quiet) )
  1978.     give_syntaxerrors = TRUE;
  1979.   else
  1980.     give_syntaxerrors = FALSE;
  1981.   if ( singles && PL_get_atom(singles, &w) && w == ATOM_warning )
  1982.     singles = TRUE;
  1983.   rval = read_term(term, varnames, subtpos, singles);
  1984.   give_syntaxerrors = osyntax;
  1985.  
  1986.   if ( rval )
  1987.   { if ( tpos && source_line_no > 0 )
  1988.       TRY(PL_unify_term(tpos,
  1989.             PL_FUNCTOR, FUNCTOR_stream_position3,
  1990.             PL_INTEGER, source_char_no,
  1991.             PL_INTEGER, source_line_no,
  1992.             PL_INTEGER, 0)); /* should be charpos! */
  1993.     if ( syntax_errors && PL_is_variable(syntax_errors) )
  1994.       return PL_unify_atom(syntax_errors, ATOM_none);
  1995.   } else
  1996.   { if ( syntax_errors && PL_is_variable(syntax_errors) )
  1997.       return PL_unify_term(syntax_errors,
  1998.                PL_FUNCTOR, FUNCTOR_module2,
  1999.                  PL_FUNCTOR, FUNCTOR_stream_position3,
  2000.                    PL_INTEGER, source_char_no,
  2001.                    PL_INTEGER, source_line_no,
  2002.                    PL_INTEGER, 0, /* should be charpos! */
  2003.                  PL_CHARS, last_syntax_error);
  2004.   }
  2005.  
  2006.   return rval;
  2007. }
  2008.  
  2009. word
  2010. pl_read_term3(term_t stream, term_t term, term_t options)
  2011. { streamInput(stream, pl_read_term(term, options));
  2012. }
  2013.